package com.bw.clock.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.widget.TextView
import com.bw.clock.R
import com.bw.clock.entity.KaoqinData
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder

import java.text.SimpleDateFormat
import java.util.*

class KaoQinAdapter : BaseQuickAdapter<KaoqinData,BaseViewHolder>(R.layout.item_attendances) {
    @SuppressLint("SimpleDateFormat")
    override fun convert(holder: BaseViewHolder, item: KaoqinData) {
        val tvTime = holder.getView<TextView>(R.id.tv_attend_time)
        tvTime.text = item.morningCheckIn

        val view = holder.getView<TextView>(R.id.tv_state)
        if (item.status.contains("1")){
            view.text = "正常"
        }else if (item.status.contains("3")){
            view.text = "迟到"
        }else if (item.status.contains("4")){
            view.text = "缺勤"
        }else if(item.status.contains("2")){
            view.text = "早退"
        }

        val drawable = GradientDrawable()
        drawable.cornerRadius = 15f
        view.background = drawable
        if (item.status.contains("1")){
            view.setTextColor(Color.parseColor("#83D362"))
            drawable.setColor(Color.parseColor("#EDF9E8"))
        }else if (item.status.contains("3")){
            view.setTextColor(Color.parseColor("#F9CB83"))
            drawable.setColor(Color.parseColor("#FDF6E9"))
        }else if (item.status.contains("2")){
            view.setTextColor(Color.parseColor("#3C9FFD"))
            drawable.setColor(Color.parseColor("#E7F3FF"))
        }else if (item.status.contains("4")){
            view.setTextColor(Color.parseColor("#FF7E7E"))
            drawable.setColor(Color.parseColor("#FFEDED"))
        }
        val inputFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
        val date11 = SimpleDateFormat("HH:mm:ss")
        val morningDate = inputFormat.parse(item.morningCheckIn)
        val afterDate = inputFormat.parse(item.afternoonCheckIn)
        if (morningDate != null && afterDate != null){
            val morning1 = date11.format(morningDate)
            val after1 = date11.format(afterDate)
            holder.getView<TextView>(R.id.tv_work_time).text = "上班 $morning1 - 下班 $after1"
        }


        if ( item.location.isNotEmpty()){
            holder.getView<TextView>(R.id.tv_address).text = item.location


        }else{
            holder.getView<TextView>(R.id.tv_address).text = "暂无位置信息"
            holder.getView<TextView>(R.id.tv_work_time).text = "未打卡"
        }


    }
}